/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.store;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import shop.core.BaseController;
import shop.core.common.bean.JSONResult;

import shop.core.common.dao.page.PagerModel;
import shop.core.util.QRcodeUtils;
import shop.services.manage.lineup.LineupService;
import shop.services.manage.lineup.bean.Lineup;
import shop.services.manage.partnerapply.PartnerapplyService;
import shop.services.manage.partnerapply.bean.Partnerapply;
import shop.services.manage.seat.SeatService;
import shop.services.manage.seat.bean.Seat;
import shop.services.manage.store.StoreService;
import shop.services.manage.store.bean.Store;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**   
 * @类名称：StoreAction      
 * @创建人：wzl
 * @创建时间：2018-01-03 下午16:50:06      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/store/")
public class StoreAction extends BaseController<Store> {
    private static final Logger logger = LoggerFactory.getLogger(StoreAction.class);
    private final  static  String PARTNERAPPLY_STATUS_PASS="pass";
    private final  static  String LINEUP_TYPE_BIG="big";
    private final  static  String LINEUP_TYPE_MIDDLE="middle";
    private final  static  String LINEUP_TYPE_SMALL="small";

    @Autowired
    private StoreService storeService;
    @Autowired
    private PartnerapplyService partnerapplyService;
    @Autowired
    private SeatService seatService;
    @Autowired
    private LineupService lineupService;

    private static final String page_toList = "/manage/store/storeList";
    private static final String page_toAdd = "/manage/store/storeAdd";
    private static final String page_toEdit = "/manage/store/storeEdit";
    public StoreService getService() {
        return storeService;
    }

    private StoreAction(){
        super.page_toList = page_toList;
        super.page_toAdd = page_toAdd;
        super.page_toEdit = page_toEdit;
    }

//    /**
//     * 查询店铺列表
//     *
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("selectStoreList")
//    @ResponseBody
//    public JSONResult selectStoreList(Store store) throws Exception {
//        List<Store> storeList = storeService.selectStoreList(store);
//        for(Store storeInfo: storeList){
//            Partnerapply partnerapply=new Partnerapply();
//            partnerapply.setStoreID(storeInfo.getId());
//            partnerapply.setStatus(PARTNERAPPLY_STATUS_PASS);
//            List<Partnerapply> partnerapplyList=partnerapplyService.selectList(partnerapply);
//            storeInfo.setPartnerAmount(partnerapplyList.size());
//        }
//        jsonResult = new JSONResult();
//        jsonResult.setData(storeList);
//        return jsonResult;
//    }

    /**
     * 查询店铺列表
     *
     * @param request 请求
     * @param store       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectStorePage")
    @ResponseBody
    public PagerModel selectStorePage(HttpServletRequest request, Store store) {
        int offset = 0;
        int pageSize = 10;
        int start=Integer.parseInt(request.getParameter("start"));
        int length= Integer.parseInt(request.getParameter("length"));
        if(start>0)
            offset=start;
        if(length>0)
            pageSize=length;
        store.setOffset(offset);
        store.setPageSize(pageSize);
        PagerModel pager = getService().selectStorePage(store);
        List<Store> storeList=pager.getList();
        for(Store storeInfo: storeList){
            Partnerapply partnerapply=new Partnerapply();
            partnerapply.setStoreID(storeInfo.getId());
            partnerapply.setStatus(PARTNERAPPLY_STATUS_PASS);
            List<Partnerapply> partnerapplyList=partnerapplyService.selectList(partnerapply);
            storeInfo.setPartnerAmount(partnerapplyList.size());
        }
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 查询店铺座位列表
     *
     * @param request 请求
     * @param seat       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectSeatPage")
    @ResponseBody
    public PagerModel selectSeatPage(HttpServletRequest request, Seat seat) {
        int offset = 0;
        int pageSize = 10;
        int start=Integer.parseInt(request.getParameter("start"));
        int length= Integer.parseInt(request.getParameter("length"));
        if(start>0)
            offset=start;
        if(length>0)
            pageSize=length;
        seat.setOffset(offset);
        seat.setPageSize(pageSize);
        PagerModel pager = seatService.selectSeatPage(seat);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 添加店铺
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "insertStore", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertStore(HttpServletRequest request,Store store) throws Exception {
        jsonResult = new JSONResult();
        int storeID=storeService.insert(store);
        QRcodeUtils qrCodeUtils = new QRcodeUtils();
        String contents = systemManager.getSystemSetting().getWww() + "/rest/front/user/toWx?menuID=" + storeID;
        store.setQrCode(qrCodeUtils.createQrcode(request, contents));
        storeService.update(store);
        return jsonResult;
    }

    /**
     * 添加店铺座位列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "insertSeat", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertSeat(HttpServletRequest request,Seat seat) throws Exception {
        jsonResult = new JSONResult();
        int seatID=seatService.insert(seat);
        QRcodeUtils qrCodeUtils = new QRcodeUtils();
        String contents = systemManager.getSystemSetting().getWww() + "/rest/front/user/toWx?seatID=" + seatID+"_"+seat.getStoreID();
        seat.setQrCode(qrCodeUtils.createQrcode(request, contents));
        seatService.update(seat);
        return jsonResult;
    }

    /**
     * 查询店铺排队记录列表
     *
     * @param request 请求
     * @param lineup       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectLineUpPage")
    @ResponseBody
    public PagerModel selectLineUpPage(HttpServletRequest request, Lineup lineup) {
        int offset = 0;
        int pageSize = 10;
        int start=Integer.parseInt(request.getParameter("start"));
        int length= Integer.parseInt(request.getParameter("length"));
        if(start>0)
            offset=start;
        if(length>0)
            pageSize=length;
        lineup.setOffset(offset);
        lineup.setPageSize(pageSize);
        PagerModel pager = lineupService.selectLineUpPage(lineup);
        int bigSeatAmount=0;int middleSeatAmount=0;int smallSeatAmount=0;
        List<Lineup> lineupList = pager.getList();
        if(lineupList!=null&&lineupList.size()>0){
            for(Lineup lineupInfo: lineupList){
                Lineup bigLineup=new Lineup();
                bigLineup.setStoreID(lineupInfo.getStoreID());
                bigLineup.setType(LINEUP_TYPE_BIG);
                bigLineup.setCreateTime(lineupInfo.getCreateTime());
                List<Lineup> bigLineupList = lineupService.selectAmountList(bigLineup);
                if(bigLineupList!=null&&bigLineupList.size()>0){
                    bigSeatAmount=bigLineupList.size();
                }
                Lineup middleLineup=new Lineup();
                middleLineup.setStoreID(lineupInfo.getStoreID());
                middleLineup.setType(LINEUP_TYPE_MIDDLE);
                middleLineup.setCreateTime(lineupInfo.getCreateTime());
                List<Lineup> middleLineupList = lineupService.selectAmountList(middleLineup);
                if(middleLineupList!=null&&middleLineupList.size()>0){
                    middleSeatAmount=middleLineupList.size();
                }
                Lineup smallLineup=new Lineup();
                smallLineup.setStoreID(lineupInfo.getStoreID());
                smallLineup.setType(LINEUP_TYPE_SMALL);
                smallLineup.setCreateTime(lineupInfo.getCreateTime());
                List<Lineup> smallLineupList = lineupService.selectAmountList(smallLineup);
                if(smallLineupList!=null&&smallLineupList.size()>0){
                    smallSeatAmount=smallLineupList.size();
                }
                lineupInfo.setBigSeatAmount(bigSeatAmount);
                lineupInfo.setMiddleSeatAmount(middleSeatAmount);
                lineupInfo.setSmallSeatAmount(smallSeatAmount);
            }
        }
        pager.setList(lineupList);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 查询店铺一天的排队记录
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectDayLineUp")
    @ResponseBody
    public JSONResult selectDayLineUp(Lineup lineup) throws Exception {
        List<Lineup> lineupList = lineupService.selectAmountList(lineup);
        jsonResult = new JSONResult();
        jsonResult.setData(lineupList);
        return jsonResult;
    }

    /**
     * 查询店铺一天的排队记录(按座位类型排序)
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectDayLineUpBySeat")
    @ResponseBody
    public JSONResult selectDayLineUpBySeat(Lineup lineup) throws Exception {
        int bigSeatAmount=0;int middleSeatAmount=0;int smallSeatAmount=0;
        List<Lineup> lineupList = lineupService.selectAmountListBySeat(lineup);
        for(Lineup lineupInfo: lineupList){
            Lineup bigLineup=new Lineup();
            bigLineup.setStoreID(lineupInfo.getStoreID());
            bigLineup.setType(LINEUP_TYPE_BIG);
            bigLineup.setCreateTime(lineupInfo.getCreateTime());
            List<Lineup> bigLineupList = lineupService.selectWaitAmountList(bigLineup);
            if(bigLineupList!=null&&bigLineupList.size()>0&&LINEUP_TYPE_BIG.equals(lineupInfo.getType())){
                bigSeatAmount=bigLineupList.size();
                lineupInfo.setBigSeatAmount(bigSeatAmount);
            }
            Lineup middleLineup=new Lineup();
            middleLineup.setStoreID(lineupInfo.getStoreID());
            middleLineup.setType(LINEUP_TYPE_MIDDLE);
            middleLineup.setCreateTime(lineupInfo.getCreateTime());
            List<Lineup> middleLineupList = lineupService.selectWaitAmountList(middleLineup);
            if(middleLineupList!=null&&middleLineupList.size()>0&&LINEUP_TYPE_MIDDLE.equals(lineupInfo.getType())){
                middleSeatAmount=middleLineupList.size();
                lineupInfo.setMiddleSeatAmount(middleSeatAmount);
            }
            Lineup smallLineup=new Lineup();
            smallLineup.setStoreID(lineupInfo.getStoreID());
            smallLineup.setType(LINEUP_TYPE_SMALL);
            smallLineup.setCreateTime(lineupInfo.getCreateTime());
            List<Lineup> smallLineupList = lineupService.selectWaitAmountList(smallLineup);
            if(smallLineupList!=null&&smallLineupList.size()>0&&LINEUP_TYPE_SMALL.equals(lineupInfo.getType())){
                smallSeatAmount=middleLineupList.size();
                lineupInfo.setSmallSeatAmount(smallSeatAmount);
            }
        }
        jsonResult = new JSONResult();
        jsonResult.setData(lineupList);
        return jsonResult;
    }

    /**
     * 删除单条排队记录
     */
    @RequestMapping(value = "deleteLineup",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteLineup(Lineup lineup){
        JSONResult jsonResult=new JSONResult();
        lineup=lineupService.selectOne(lineup);
        Lineup lineupInfo=new Lineup();
        lineupInfo.setStoreID(lineup.getStoreID());
        lineupInfo.setCreateTime(lineup.getCreateTime());
        lineupService.delete(lineupInfo);
        return jsonResult;
    }

    /**
     * 批量删除排队记录
     */
    @RequestMapping(value = "deleteLineups",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteLineups(@RequestParam(value = "ids[]") String[] ids){
        JSONResult jsonResult=new JSONResult();
        for(int i=0;i<ids.length;i++){
            Lineup lineup=new Lineup();
            lineup.setId(ids[i]);
            lineup=lineupService.selectOne(lineup);
            Lineup lineupInfo =new Lineup();
            lineupInfo.setStoreID(lineup.getStoreID());
            lineupInfo.setCreateTime(lineup.getCreateTime());
            lineupService.delete(lineupInfo);
        }
        return jsonResult;
    }

    /**
     * 更新排队记录（下一位）
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "updateLineup", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateLineup(Lineup lineup) throws Exception {
        jsonResult = new JSONResult();
        lineup.setIsNow("n");
        lineupService.update(lineup);
        lineup=lineupService.selectOne(lineup);
        List<Lineup> lineupList = lineupService.selectWaitAmountList(lineup);
        if(lineupList!=null&&lineupList.size()>0){
            lineupList.get(0).setIsNow("y");
            lineupService.update(lineupList.get(0));
        }
        return jsonResult;
    }
}
